﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace SHFST.Agents
{
   class Enemy : Agent
   {
      public bool active;
      SoundEffectInstance salmonHit;
      SoundEffectInstance enemyHit;

      public void Load (ContentManager content, Texture2D newTexture, Point newFrameSize, Point newSheetSize, Point newPos, int newLife)
      {
         base.Load (newTexture, newFrameSize, newSheetSize, newPos);

         salmonHit = content.Load<SoundEffect> (".\\Sounds\\Agents\\SalmonHit").CreateInstance ();
         enemyHit = content.Load<SoundEffect> (".\\Sounds\\Agents\\EnemyHit").CreateInstance ();

         life = newLife;
         active = false;
      }

      public bool Update (GraphicsDevice device, Salmon salmon, int gameProgress, int speed, int time)
      {
         if (!active && gameProgress >= position.X)
         {
            active = true;
            position.X = device.Viewport.Width;
            if (position.Y == 4)
            {
               position.Y = device.Viewport.Height * position.Y / 4 - sprite.frameSize.Y;
            }
            else
            {
               position.Y = device.Viewport.Height * position.Y / 4;
            }
         }
         else if (active)
         {
            if (position.X <= -sprite.frameSize.X)
            {
               return false;
            }

            base.Update (time);

            bool survive = true;
            if (Collision (salmon.bullet))
            {
               enemyHit.Play ();
               salmon.bullet.kill = true;
               salmon.score += 10 * life;
               if (--life <= 0)
               {
                  survive = false;
               }
            }
            if (Collision (salmon))
            {
               salmon.score -= 10 * life;
               if (salmon.score < 0)
               {
                  salmon.score = 0;
               }

               --salmon.life;
               if (salmon.life > 0)
               {
                  salmonHit.Play ();
               }

               survive = false;
            }

            if (survive)
            {
               position.X += speed;
            }

            return survive;
         }

         return true;
      }
   }
}
